﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using BLL;
using Model;

namespace Inventory.view
{
    public partial class GoodsClassfy : System.Web.UI.Page
    {
        public int sellerId;
        private GoodsClassfyBll gc = new GoodsClassfyBll();
        private Common cmm = new Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            Seller model = (Seller)Session["CurrentUser"];
            if (model == null)
            {
                //Response.Write("<script>alert(\"请登录系统\");window.location.href=\"login.aspx\"</script>");
                //Response.End();
            }
            //sellerId = model.Id;
            if (Request["action"] != null && Request["action"] == "getJson")//获取父类
            {
                GetJson();
            }
            else if (Request["action"] != null && Request["action"] == "getchild")
            {
                GetChildClassfy();
            }
            else if (Request["action"] != null && Request["action"] == "save")
            {
                Save();
            }
        }

        private void GetJson()
        {
            List<MGoodsClassfy> list = new List<MGoodsClassfy>();
            list = gc.ListClass(" and parentId=0 ");
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();//对时间进行格式化
            timeConverter.DateTimeFormat = "yyyy-MM-dd";
            string jsonText = JsonConvert.SerializeObject(list, Formatting.Indented, timeConverter);
            Response.Write(jsonText);
            Response.End();
        }

        //得到子分类
        private void GetChildClassfy()
        {
            List<MGoodsClassfy> list = new List<MGoodsClassfy>();
            list = gc.ListClass(" and parentId=" + Request["parentId"]);
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();//对时间进行格式化
            timeConverter.DateTimeFormat = "yyyy-MM-dd";
            string jsonText = JsonConvert.SerializeObject(list, Formatting.Indented, timeConverter);
            Response.Write(jsonText);
            Response.End();
        }

        private void Save()
        {
            try
            {
                List<MGoodsClassfy> list = new List<MGoodsClassfy>();
                list = JsonConvert.DeserializeObject<List<MGoodsClassfy>>(Request["jsonStr"]);
                gc.Save(list, Request["delids"], sellerId);
                Response.Write("保存成功");
                Response.End();
            }
            catch(Exception ex)
            {

            }
        }
    }
}
